package com.example.f8399.testdemo.activity;

import android.animation.ObjectAnimator;
import android.databinding.DataBindingUtil;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;

import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.example.caughtglobalexceptionlibrary.CosmosException;
import com.example.f8399.testdemo.BaseActivity;
import com.example.f8399.testdemo.R;
import com.example.f8399.testdemo.databinding.ActivityFunctionBinding;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import transferObject.TransferObject;

/**
 * 查询各种的函数功能
 * */
public class FunctionActivity extends AppCompatActivity {

    ActivityFunctionBinding mBinding;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding =DataBindingUtil.setContentView(this,R.layout.activity_function);
        initView();
    }

    private void initView() {

        mBinding.cbShowPass.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                changeVisible(mBinding.etPass,b);
            }
        });
    }

    public void curList(View view) {
        List<String> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            list.add(String.valueOf(i));
        }
        //打印0,1,2
        LogUtils.d(list.subList(0,3));
        //打印0,1,2
        list.subList(3,list.size()).clear();
        LogUtils.d(list);
    }

    public void countdown(View view) {
        final Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        ToastUtils.showShort("hello,当你看到这条信息时已经过去2秒了");
                        timer.cancel();
                    }
                });
            }
        }, 2000);
    }

    private int count =0 ;
    public void countdown2(final View view) {
        final Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                if (count < 5) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            ((Button)view).setText("倒计时"+count);
                        }
                    });
                    ++count;
                }else {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            ((Button)view).setText("(๑•̀ㅂ•́)و✧");

                        }
                    });
                    timer.cancel();
                }
            }
        }, 0,1000);
    }

    public void rotate(View view) {
        ObjectAnimator animator = ObjectAnimator.ofFloat(view, "rotation", 0f, 360f);
        animator.setDuration(5000);
        animator.start();
    }



    private void changeVisible(View view, boolean isVisible) {
        EditText text = view instanceof EditText ? ((EditText) view) : null;
        if (text != null) {
            if (isVisible) {
                text.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
            }else {
                text.setTransformationMethod(PasswordTransformationMethod.getInstance());
            }
        }
    }

    public void installApp(View view) {
        AppUtils.installApp(new File(Environment.getExternalStorageDirectory()+"/Download/alarm.apk"));
    }

    public void showException(View view) {
        /*try {

            throw new RuntimeException("这个是我自己的异常");
        } catch (RuntimeException e) {
            return;
        }finally {
            ToastUtils.showShort("你说怎么办吧");
        }*/

        throw new RuntimeException("我自己创造的异常");

    }


    public void checkMd5File(View view) {
        String md5ToString = FileUtils.getFileMD5ToString(Environment.getExternalStorageDirectory() + "/Download/b.mp4");
        if (md5ToString.equals("C2FB826B7B651C69043F96AE2CFC8C85")) {
            ToastUtils.showShort("一样的哟");
        }else {
            ToastUtils.showShort("不一样，草");
        }
    }

}
